<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="500px">
			<el-form ref="formRef" :model="state.ruleForm" label-width="auto">
				<el-button type="primary" @click="onAddLogistics"> 添加物流物件 </el-button>
				<div v-for="(item, index) in state.ruleForm.logistics_list" :key="item.id">
					<div style="display: flex">
						<el-divider content-position="left">物流物件{{ index + 1 }}</el-divider>
						<el-button v-if="index" class="ml5" type="primary" link @click="onDelete(index)">删除</el-button>
					</div>
					<el-form-item
						label="快递公司"
						:prop="`logistics_list[${index}].logistics_name`"
						:rules="[{ required: true, message: `快递公司不能为空`, trigger: ['blur', 'change'] }]"
					>
						<el-select
							v-model="state.ruleForm.logistics_list[index].logistics_name"
							filterable
							clearable
							placeholder="请选择快递公司"
						>
							<el-option v-for="item in state.freightNumberSelect" :key="item.id" :label="item.name" :value="item.name" />
						</el-select>
					</el-form-item>
					<el-form-item
						label="快递单号"
						:prop="`logistics_list[${index}].logistics_no`"
						:rules="[{ required: true, message: `快递单号不能为空`, trigger: ['blur'] }]"
					>
						<el-input
							type="text"
							v-model="state.ruleForm.logistics_list[index].logistics_no"
							clearable
							placeholder="请输入快递单号"
						/>
					</el-form-item>
				</div>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提 交</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { nextTick, reactive, ref } from 'vue';

import { logisticsApi, orderApi } from '/@/api/supplier';

let callback: Function = () => {};
const OrderApi = orderApi();
const LogisticsApi = logisticsApi();
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	},
	ruleForm: {
		order_detail_id: '',
		logistics_list: [
			{
				id: 1,
				logistics_name: '',
				logistics_no: '',
				type: 'add',
			},
		],
	},
	rules: {
		logistics_name: [{ required: true, message: '快递公司不能为空', trigger: ['blur', 'change'] }],
		logistics_no: [{ required: true, message: '快递单号不能为空', trigger: 'blur' }],
	},
	freightNumberSelect: [] as any,
});
const formRef = ref();

/********** 触发事件 **********/
// 添加物流物件
const onAddLogistics = () => {
	state.ruleForm.logistics_list.push({
		id: state.ruleForm.logistics_list.length + 1,
		logistics_name: '',
		logistics_no: '',
		type: 'add',
	});
};
// 删除物流物件
const onDelete = (index: number) => {
	state.ruleForm.logistics_list.splice(index, 1);
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		state.dialog.submitLoading = true;
		const logistics_info = state.ruleForm.logistics_list.map((item) => `${item.logistics_name}:${item.logistics_no}`).join(',');
		const params = {
			order_detail_id: state.ruleForm.order_detail_id,
			logistics_info: logistics_info,
		};
		_orderDeliverNew(params);
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (data: any, params: any, fn: Function) => {
	state.dialog = {
		isShowDialog: true,
		type: data.type,
		title: data.title,
		submitLoading: false,
	};
	callback = fn;
	_logisticsList({ pageNum: 1, pageSize: 200 });
	nextTick(() => {
		const logistics_list =
			state.dialog.type === 'add'
				? [
						{
							id: 1,
							logistics_name: '',
							logistics_no: '',
							type: 'add',
						},
				]
				: params.orders.logistics;
		state.ruleForm = {
			order_detail_id: params.orders.id,
			logistics_list: JSON.parse(JSON.stringify(logistics_list)),
		};
	});
};

/********** 网络请求 **********/
// 物流公司列表
const _logisticsList = (params: Object) => {
	LogisticsApi.logisticsList(params)
		.then((res) => {
			state.freightNumberSelect = res.data.list;
		})
		.catch(() => {});
};
// 订单发货(新)
const _orderDeliverNew = (params: object) => {
	OrderApi.orderDeliverNew(params)
		.then(() => {
			ElMessage.success('操作成功');
			onCancel();
			callback();
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
